package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.PlatformEnumForPublish;
import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.pojos.Banner;
import com.jilefojing.beans.query.BannerQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IBannerDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IBannerMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月19日 13时11分12秒
 */
@Repository
public class BannerDao extends BaseDao<Banner> implements IBannerDao {
	@Autowired
	private IBannerMapper bannerMapper;

	@Override
	public Pagination<Banner> getPagination(BannerQuery bannerQuery, Pagination<Banner> pagination) {
		Example example = new Example(Banner.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(bannerQuery.getIds())) {
			criteria.andIn(Banner.FIELD_ID, bannerQuery.getIds());
		}

		if (Detect.notEmpty(bannerQuery.getStatuses())) {
			List<Short> statusVals = new ArrayList<Short>();
			for (StatusEnumForPublish _Status : bannerQuery.getStatuses()) {
				statusVals.add(_Status.getCode());
			}
			criteria.andIn(Banner.FIELD_STATUS, statusVals);
		}

		if (Detect.notEmpty(bannerQuery.getPlatforms())) {
			List<Short> platformVals = new ArrayList<Short>();
			for (PlatformEnumForPublish _Platform : bannerQuery.getPlatforms()) {
				platformVals.add(_Platform.getCode());
			}
			criteria.andIn(Banner.FIELD_PLATFORM, platformVals);
		}

		if (bannerQuery.getEqualStartTime() != null) {
			criteria.andEqualTo(Banner.FIELD_STARTTIME, bannerQuery.getEqualStartTime());
		}

		if (bannerQuery.getEqualAndBeforStartTime() != null) {
			criteria.andGreaterThanOrEqualTo(Banner.FIELD_STARTTIME, bannerQuery.getEqualAndBeforStartTime());
		}

		if (bannerQuery.getEqualAndAfterStartTime() != null) {
			criteria.andLessThanOrEqualTo(Banner.FIELD_STARTTIME, bannerQuery.getEqualAndAfterStartTime());
		}

		if (bannerQuery.getBeforStartTime() != null) {
			criteria.andGreaterThan(Banner.FIELD_STARTTIME, bannerQuery.getBeforStartTime());
		}

		if (bannerQuery.getAfterStartTime() != null) {
			criteria.andLessThan(Banner.FIELD_STARTTIME, bannerQuery.getAfterStartTime());
		}

		if (bannerQuery.getEqualEndTime() != null) {
			criteria.andEqualTo(Banner.FIELD_ENDTIME, bannerQuery.getEqualEndTime());
		}

		if (bannerQuery.getEqualAndBeforEndTime() != null) {
			criteria.andGreaterThanOrEqualTo(Banner.FIELD_ENDTIME, bannerQuery.getEqualAndBeforEndTime());
		}

		if (bannerQuery.getEqualAndAfterEndTime() != null) {
			criteria.andLessThanOrEqualTo(Banner.FIELD_ENDTIME, bannerQuery.getEqualAndAfterEndTime());
		}

		if (bannerQuery.getBeforEndTime() != null) {
			criteria.andGreaterThan(Banner.FIELD_ENDTIME, bannerQuery.getBeforEndTime());
		}

		if (bannerQuery.getAfterEndTime() != null) {
			criteria.andLessThan(Banner.FIELD_ENDTIME, bannerQuery.getAfterEndTime());
		}

		if (bannerQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(Banner.FIELD_CREATETIME, bannerQuery.getEqualCreateTime());
		}

		if (bannerQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Banner.FIELD_CREATETIME, bannerQuery.getEqualAndBeforCreateTime());
		}

		if (bannerQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(Banner.FIELD_CREATETIME, bannerQuery.getEqualAndAfterCreateTime());
		}

		if (bannerQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(Banner.FIELD_CREATETIME, bannerQuery.getBeforCreateTime());
		}

		if (bannerQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(Banner.FIELD_CREATETIME, bannerQuery.getAfterCreateTime());
		}

		if (bannerQuery.getEqualUpdateTime() != null) {
			criteria.andEqualTo(Banner.FIELD_UPDATETIME, bannerQuery.getEqualUpdateTime());
		}

		if (bannerQuery.getEqualAndBeforUpdateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Banner.FIELD_UPDATETIME, bannerQuery.getEqualAndBeforUpdateTime());
		}

		if (bannerQuery.getEqualAndAfterUpdateTime() != null) {
			criteria.andLessThanOrEqualTo(Banner.FIELD_UPDATETIME, bannerQuery.getEqualAndAfterUpdateTime());
		}

		if (bannerQuery.getBeforUpdateTime() != null) {
			criteria.andGreaterThan(Banner.FIELD_UPDATETIME, bannerQuery.getBeforUpdateTime());
		}

		if (bannerQuery.getAfterUpdateTime() != null) {
			criteria.andLessThan(Banner.FIELD_UPDATETIME, bannerQuery.getAfterUpdateTime());
		}

		if (Detect.notEmpty(bannerQuery.getTitle())) {
			criteria.andEqualTo(Banner.FIELD_TITLE, bannerQuery.getTitle());
		}

		if (Detect.notEmpty(bannerQuery.getLike_title())) {
			criteria.andLike(Banner.FIELD_TITLE, "%" + bannerQuery.getLike_title() + "%");
		}

		if (Detect.notEmpty(bannerQuery.getCreateUserIds())) {
			criteria.andIn(Banner.FIELD_CREATEUSER, bannerQuery.getCreateUserIds());
		}
		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}
		return super.getPagination(pagination, example);
	}

	@Override
	public Banner getById(long id) {
		List<Banner> banners = this.getListByIds(Arrays.asList(new Long[] { id }));
		if (!Detect.notEmpty(banners)) {
			return null;
		}
		return banners.get(0);
	}

	@Override
	public List<Banner> getListByIds(List<Long> ids) {
		Example example = new Example(Banner.class);
		example.createCriteria().andIn(Banner.FIELD_ID, ids);
		return super.getListByExample(example);
	}
}
